package authorization_code_postgres

import (
	"net/url"
	"strconv"
	"time"

	"github.com/go-oauth2/oauth2/v4"
)

// 数据模型定义
// Client 客户端模型
type Client struct {
	ID        string `gorm:"primaryKey"`
	Secret    string
	Domain    string
	UserID    uint
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (c *Client) GetUserID() string {
	return strconv.Itoa(int(c.UserID))
}

func (c *Client) GetID() string {
	return c.ID
}
func (c *Client) GetSecret() string {
	return c.Secret
}
func (c *Client) GetDomain() string {
	return c.Domain
}
func (c *Client) IsPublic() bool {
	return true
}

// Token OAuth2 令牌模型
type Token struct {
	ID               uint `gorm:"primaryKey"`
	ClientID         string
	UserID           uint
	RedirectURI      string
	Scope            string
	Code             string `gorm:"index"`
	CodeCreateAt     time.Time
	CodeExpiresIn    time.Time
	Access           string `gorm:"index"`
	AccessCreateAt   time.Time
	AccessExpiresIn  time.Time
	Refresh          string `gorm:"index"`
	RefreshCreateAt  time.Time
	RefreshExpiresIn time.Time
}

func (t *Token) New() oauth2.TokenInfo {
	return &Token{}
}

func (t *Token) GetExtension() url.Values {
	return url.Values{}
}

func (t *Token) SetExtension(values url.Values) {
}

func (t *Token) GetClientID() string {
	return t.ClientID

}

func (t *Token) SetClientID(s string) {
	t.ClientID = s

}

func (t *Token) GetUserID() string {
	return strconv.Itoa(int(t.UserID))

}

func (t *Token) SetUserID(s string) {
	atoi, err := strconv.Atoi(s)
	if err != nil {
		atoi = 1
	}
	t.UserID = uint(atoi)

}

func (t *Token) GetRedirectURI() string {
	return t.RedirectURI

}

func (t *Token) SetRedirectURI(s string) {
	t.RedirectURI = s

}

func (t *Token) GetScope() string {
	return t.Scope

}

func (t *Token) SetScope(s string) {
	t.Scope = s

}

func (t *Token) GetCode() string {
	return t.Code

}

func (t *Token) SetCode(s string) {
	t.Code = s

}

func (t *Token) GetCodeCreateAt() time.Time {
	return t.CodeCreateAt

}

func (t *Token) SetCodeCreateAt(t2 time.Time) {
	t.CodeCreateAt = t2

}

func (t *Token) GetCodeExpiresIn() time.Duration {
	return t.CodeExpiresIn.Sub(t.CodeCreateAt)

}

func (t *Token) SetCodeExpiresIn(duration time.Duration) {
	t.CodeExpiresIn = t.CodeCreateAt.Add(duration)

}

func (t *Token) GetCodeChallenge() string {
	return ""

}

func (t *Token) SetCodeChallenge(s string) {
}

func (t *Token) GetCodeChallengeMethod() oauth2.CodeChallengeMethod {
	return ""

}

func (t *Token) SetCodeChallengeMethod(method oauth2.CodeChallengeMethod) {
}

func (t *Token) GetAccess() string {
	return t.Access

}

func (t *Token) SetAccess(s string) {
	t.Access = s

}

func (t *Token) GetAccessCreateAt() time.Time {
	return t.AccessCreateAt

}

func (t *Token) SetAccessCreateAt(t2 time.Time) {
	t.AccessCreateAt = t2

}

func (t *Token) GetAccessExpiresIn() time.Duration {
	return t.AccessExpiresIn.Sub(t.AccessCreateAt)
}

func (t *Token) SetAccessExpiresIn(duration time.Duration) {
	t.AccessExpiresIn = t.AccessCreateAt.Add(duration)

}

func (t *Token) GetRefresh() string {
	return t.Refresh

}

func (t *Token) SetRefresh(s string) {
	t.Refresh = s

}

func (t *Token) GetRefreshCreateAt() time.Time {
	return t.RefreshCreateAt

}

func (t *Token) SetRefreshCreateAt(t2 time.Time) {
	t.RefreshCreateAt = t2

}

func (t *Token) GetRefreshExpiresIn() time.Duration {
	return t.RefreshExpiresIn.Sub(t.RefreshCreateAt)
}

func (t *Token) SetRefreshExpiresIn(duration time.Duration) {
	t.RefreshExpiresIn = t.RefreshCreateAt.Add(duration)
}
